//
//  VVideoPickVC.m
//  VChatVideoClip
//
//  Created by huangbolun on 2017/10/9.
//  Copyright © 2017年 huangbolun. All rights reserved.
//

#import "VVideoPickVC.h"
#import "VVideoClipManger.h"
#import "VVideoPickCell.h"
#import "VClipVideoVC.h"

@interface VVideoPickVC ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property(nonatomic, strong) UICollectionView *collectionView;

@property(nonatomic, strong) PHFetchResult *videoData;

@end

@implementation VVideoPickVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"选择视频";
    self.view.backgroundColor = [UIColor whiteColor];
    [self setupCollectionView];
    [self setupData];
}

- (void)setupData{
    [[VVideoClipManger shareClipManger] getVideoListWithCompelet:^(PHFetchResult *result) {
        self.videoData = result;
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
           [self.collectionView reloadData];
        }];
    }];
}

- (void)setupCollectionView{
    UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
    layout.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width / 4, [UIScreen mainScreen].bounds.size.width / 4);
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layout];
    [self.view addSubview:self.collectionView];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor clearColor];
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"VVideoPickCell" bundle:nil] forCellWithReuseIdentifier:PICK_CELL];
}


#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.videoData.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    VVideoPickCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:PICK_CELL forIndexPath:indexPath];
    cell.videoAsset = self.videoData[indexPath.item];
    return cell;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    PHAsset *asset = self.videoData[indexPath.item];
    VClipVideoVC *vc = [VClipVideoVC new];
    vc.videoAsset = asset;
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    [self presentViewController:nav animated:YES completion:nil];
    
}

@end
